<?php

    /**
     * @package      Modules
     * @subpackage   Languages_Classes_Db
	 *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class DbCountryLanguage extends BaseDbCountryLanguage
    {

    ////////////////////////////////////////////////////////////////////////////
	//                                Methods                                 //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Returns the list of languages by country
         *
         * @param       boolean     $displayInFront
         * @param       boolean     $displayInBack
         * @return      array
         */
        private static function _getLanguages($displayInFront = null, $displayInBack = null)
        {
            $languagesQuery = Doctrine_Query::create()
                                ->from(__CLASS__ . ' cl')
                                ->leftJoin('cl.DbLanguage l')
                                ->leftJoin('cl.DbCountry c');

            // Filter by displayInFront param
            if ($displayInFront !== null) {
                $languagesQuery->addWhere('displayInFront = ?', $displayInFront);
            }

            // Filter by displayInBack param
            if ($displayInBack !== null) {
                $languagesQuery->addWhere('displayInBack = ?', $displayInBack);
            }

            // Execute the query
            $languages      = $languagesQuery->execute(array(), Doctrine::HYDRATE_ARRAY);

            // Builds the array to return by ordering languages by countries
            $languageByCountry  = array();

            // Loops on language / country associations
            foreach ($languages as $language) {
                
                if (isset($language['DbCountry']) && isset($language['DbLanguage'])) {

                    $_countryKey    = $language['DbCountry']['code'];
                    $_languageKey   = $language['DbLanguage']['code'];

                    // Adds the country if not exists yet
                    if (!isset($languageByCountry[$_countryKey])) {
                        $languageByCountry[$_countryKey]                = $language['DbCountry'];
                        $languageByCountry[$_countryKey]['DbLanguage']  = array();
                    }

                    // Adds the language to the country
                    $languageByCountry[$_countryKey]['DbLanguage'][$_languageKey]   = $language['DbLanguage'];
                }
            }

            return $languageByCountry;
        }

        /**
         * Returns the list of languages displayed in back groupped by country
         *
         * @return      array
         */
        public static function getBackLanguages()
        {
            return self::_getLanguages(null, true);
        }

        /**
         * Returns the list of languages displayed in front groupped by country
         *
         * @return      array
         */
        public static function getFrontLanguages()
        {
            return self::_getLanguages(true, null);
        }
    }